<?php
class Moderate extends LibertyContent {

	function Moderate() {
		LibertyContent::LibertyContent();
	}

	function revert($pContentId,$smart=true) {
		//$lObj = new LibertyContent();
		$lObj = LibertyBase::getLibertyObject($pContentId);
		$h = $lObj->getHistory();
		$oh = array();
		foreach ($h as $version) {
			$oh[$version['version']]['rate']['v_stars_rating']=$version['v_stars_rating'];
			$oh[$version['version']]['rate']['v_stars_rating_count']=$version['v_stars_rating_count'];
			$oh[$version['version']]['rate']['stars_rating']=$version['stars_rating'];
			$oh[$version['version']]['rate']['stars_rating_count']=$version['stars_rating_count'];
			if (!empty($version['v_stars_rating_count'])) {
				$oh[$version['version']]['rate']['v'] =$version['v_stars_rating'];
				$oh[$version['version']]['rate']['c'] =$version['stars_rating'];
			} else {
				$oh[$version['version']]['rate']['v'] =false;
				$oh[$version['version']]['rate']['c'] =false;
			}
			$oh[$version['version']]['data']=$version;
		}
		ksort($oh);
		$versions = array_keys($oh);
		foreach ($versions as $k => $v) {
			if ($k>0 && $oh[$v]['rate']['v'] === false) {
				$oh[$v]['rate']['c'] = $oh[$versions[$k-1]]['rate']['c'];
				$oh[$v]['rate']['v'] = $oh[$versions[$k-1]]['rate']['v'];
			}
			if ($oh[$v]['rate']['v'] === false) {
				$oh[$v]['rate']['c'] = 100;
				$oh[$v]['rate']['v'] = 100;
			}
		}
		krsort($oh);
		$versions = array_keys($oh);
		$min_index = $versions[sizeof($versions)-1];
		$revert_v = $versions[0];
		if ($smart) {
			foreach ($versions as $v) {
				$v_1 = $v-1;
				if ($v_1>$min_index) {
					if ($oh[$v]['rate']['v'] > $oh[$v-1]['rate']['v']) {
						$revert_v=$v;
						break;
					}
				} else {
					$revert_v=$v;
					break;
				}
			}
		}
		$lObj->rollbackVersion($revert_v,"(Moderatation)");
	}

	function getList($pListHash) {
		$l = parent::getContentList($pListHash);
		$l=$l['data'];
		$lData = array();
		foreach ($l as $i) {
			$t = $this->getType($i);
			if ($t['show']) {
				$i['moderate_type']=$t;
				$lData[]=$i;
			}
		}
		return $lData;
	}

	function getType($pServHash) {
		global $gLibertySystem;
		$t = array('spam'=>false,'offensive'=>false,'illegal'=>false,'show'=>false);

		$obj = $gLibertySystem->getLibertyObject($pServHash['content_id']);
		if (!empty($obj)) {
			foreach (array_keys($t) as $type) {
				$p = $obj->getPreference("mod_$type",0);
				$p= !empty($p);
				if ($p) {
					$t[$type] = true;
					$t['show'] = true;
				}
			}
		}

		if (needsModeratation($pServHash)) {
			$t['mod'] = true;
			$t['show'] = true;
		} elseif ($t['illegal']) {
			$t['show'] = true;
		}


		return $t;
	}

	function needsModeratation($pServHash) {
		global $gLibertySystem;
		global $gBitSystem;

		$rating_vars = $gLibertySystem->invokeServiceFunctionR('content_get_rating_feild_function',array(null,false));
		$rating_ver_vars = $gLibertySystem->invokeServiceFunctionR('content_get_rating_feild_function',array(null,true));
		$rating_count_vars = $gLibertySystem->invokeServiceFunctionR('content_get_rating_feild_function',array(null,false,true));
		$rating_count_ver_vars = $gLibertySystem->invokeServiceFunctionR('content_get_rating_feild_function',array(null,true,true));
		$mod = false;
		foreach (array_keys($rating_vars) as $serv) {
			$rating = $pServHash[$rating_vars[$serv]];
			$rating_v = $pServHash[$rating_ver_vars[$serv]];
			$rating_count = $pServHash[$rating_count_vars[$serv]];
			$rating_v_count = $pServHash[$rating_count_ver_vars[$serv]];
			$mod = $mod || Moderate::needsModeratationCheck($rating,$rating_v,$rating_count,$rating_v_count);
		}
		return ($mod);
	}

	function needsModeratationCheck($rating,$rating_v,$rating_count,$rating_v_count) {
		global $gBitSystem;
		$mVerMin = $gBitSystem->getConfig("moderate_ver_min",50);
		$mMin = $gBitSystem->getConfig("moderate_min",20);
		$mVerMinC = $gBitSystem->getConfig("moderate_ver_min_c",5);
		$mMinC = $gBitSystem->getConfig("moderate_min_c",12);
		$mMaxF = $gBitSystem->getConfig("moderate_max_fallback",0);

		$mod = false;

		if ($rating_count<$mMinC) {
			unset($rating);
		}

		if ($rating_v_count<$mVerMinC) {
			unset($rating_v);
		}

		if (empty($rating_v) && !empty($rating) && $rating< $mMin) {
			$mod=true;
		} elseif (!empty($rating_v) && $rating_v< $mVerMin ) {
			$mod=true;
		} elseif (!empty($rating) && !empty($rating_v) && ($rating-$rating_v)>$mMaxF) {
			$mod=true;
		}

		return $mod;

	}
}

function getModerationType($pServHash) {
	return Moderate::getType($pServHash);
}

function needsModeratation($pServHash) {
	return Moderate::needsModeratation($pServHash);
}

function moderate_content_list_sql( &$pObject ) {
	global $gBitSystem, $gBitUser, $gBitSmarty, $gLibertySystem;
	$ret = array('where_sql'=>"");
	if( !$gBitUser->hasPermission( 'p_moderate_view') ) {
		$mVerMin = $gBitSystem->getConfig("moderate_sql_ver_min",40);
		$mMin = $gBitSystem->getConfig("moderate_sql_min",10);
		$mVerMinC = $gBitSystem->getConfig("moderate_sql_ver_min_c",7);
		$mMinC = $gBitSystem->getConfig("moderate_sql_min_c",15);
		$mMaxF = $gBitSystem->getConfig("moderate_sql_max_fallback",0);
		$rating_vars = $gLibertySystem->invokeServiceFunctionR('content_get_rating_feild_function',array($pObject,false,false,true));
		$rating_ver_vars = $gLibertySystem->invokeServiceFunctionR('content_get_rating_feild_function',array($pObject,true,false,true));
		$rating_count_vars = $gLibertySystem->invokeServiceFunctionR('content_get_rating_feild_function',array($pObject,false,true,true));
		$rating_count_ver_vars = $gLibertySystem->invokeServiceFunctionR('content_get_rating_feild_function',array($pObject,true,true,true));
		foreach ($rating_vars as $serv => $rate_var) {
			$rate_v_var = $rating_ver_vars[$serv];
			$rate_c_var = $rating_count_vars[$serv];
			$rate_c_v_var = $rating_count_ver_vars[$serv];
			if (!empty($rate_v_var)) {
				$ret['where_sql'] .= " (($rate_var IS NULL) OR ($rate_var>10 AND ($rate_v_var IS NULL OR ($rate_v_var>50
				AND ($rate_var - $rate_v_var) < $mMaxF))))";
			} elseif (!empty($rate_var)) {
				$ret['where_sql'] .= " ($rate_var IS NULL OR $rate_var>10)";
			}

			if(empty($rate_c_var)) {
			} elseif(empty($rate_c_v_var)) {
				$ret['where_sql'] = " (($rate_c_var < $mMinC) OR ".$ret['where_sql'].")";
			} else {
				$ret['where_sql'] = " (($rate_c_v_var < $mVerMinC AND $rate_c_var < $mMinC) OR ".$ret['where_sql'].")";
			}
			$ret['where_sql'] = trim($ret['where_sql']);
			if (!empty($ret['where_sql'])) {
				$ret['where_sql'] = " AND ".$ret['where_sql'] ;
			}
		}
	}
	return $ret;
}

function moderate_content_load_sql( &$pObject ) {
	global $gBitSystem, $gBitUser, $gBitSmarty;
	$ret = moderate_content_list_sql(&$pObject);
	/*
	$ret = array('where_sql'=>"");
	if( !$gBitUser->hasPermission( 'p_moderate_view') ) {
	$s_f = Moderate::invokeServiceFunctionR('content_get_rating_feild_function',array(false,false,true));
	$sv_f = Moderate::invokeServiceFunctionR('content_get_rating_feild_function',array(true,false,true));
	foreach ($s_f as $serv => $rate_var) {
	$rate_v_var = $sv_f[$serv];
	if (!empty($rate_v_var)) {
	$ret['where_sql'] .= " AND (($rate_var IS NULL) OR ($rate_var>10 AND ($rate_v_var IS NULL OR ($rate_v_var>50
	AND $rate_v_var>$rate_var))))";
	} else {
	$ret['where_sql'] .= " AND ($rate_var IS NULL OR $rate_var>10)";
	}
	}
	}*/
	return $ret;
}

function moderate_content_rating_updated($pContentId,$paramHash) {
	global $gBitSystem, $gBitUser, $gLibertySystem;
	if ($gBitSystem->isFeatureActive('moderate_autorevert') && $gBitUser->hasPermission('p_moderate_preform') && $gLibertySystem->needsModeratation($paramHash)) {
		$m = new Moderate();
		$m->revert($pContentId,true);
	}
}
?>
